package com.turman.coroutionsample.view

import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.turman.coroutionsample.ui.theme.CoroutionSampleTheme
import com.turman.coroutionsample.viewmodel.UserViewModel


/**
 * 数据插入后 要能够立即查询到并刷新至列表，这就形同一个上游下游的关系
 */
@Preview
@Composable
fun RoomFlowPage() {
    var userId by remember {
        mutableStateOf("")
    }
    var userFirstName by remember {
        mutableStateOf("")
    }
    var userLastName by remember {
        mutableStateOf("")
    }
    val context = LocalContext.current
    var viewmodel = viewModel<UserViewModel>()

    //通过flow获取的数据列表不需要在viewmodel中通过livedata和view交互，方便多了！
    //数据插入以后可以直接获取，无需先insert 再去 getAll
    val userList = viewmodel.getAll().collectAsState(null)

//    val userList1 by viewmodel.userList.observeAsState()

    CoroutionSampleTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.background
        ) {
            Column {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceEvenly
                ) {
                    TextField(
                        value = userId,
                        onValueChange = { userId = it },
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
                    )
                    TextField(value = userFirstName, onValueChange = { userFirstName = it })
                    TextField(value = userLastName, onValueChange = { userLastName = it })
                }
                Spacer(modifier = Modifier.height(10.dp))
                Button(
                    onClick = {
                        //添加逻辑
                        if (userId.isNullOrBlank()) {
                            Toast.makeText(context, "id必须为数字", Toast.LENGTH_SHORT).show()
                            return@Button
                        }
                        viewmodel.insert(userId, userFirstName, userLastName)
                    },
                    modifier = Modifier
                        .fillMaxWidth(0.8f)
                        .align(Alignment.CenterHorizontally)
                ) {
                    Text(text = "ADD")
                }
                Spacer(modifier = Modifier.height(10.dp))
                LazyColumn(
                    Modifier.fillMaxWidth(),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    items(userList.value?.size ?: 0) {
                        Text(
                            text = "${userList.value?.get(it)?.uid},${
                                userList.value?.get(it)?.firstName
                            },${
                                userList.value?.get(
                                    it
                                )?.lastName
                            }"
                        )

//                        Text(
//                            text = "${userList1?.get(it)?.uid},${
//                                userList1?.get(it)?.firstName
//                            },${
//                                userList1?.get(
//                                    it
//                                )?.lastName
//                            }"
//                        )
                    }
                }
            }
        }
    }
}